<template>
  <div style="overflow-y: auto; height: 100%">
    <div v-show="inMaterial">
      <FormTitle
        title="红线内进度"
        @change="(val) => toggle(val, 'inside')"
        :showToggle="!!list.length"
      />
      <BasicTable @register="registerTable">
        <template #headerCell="{ column }">
          <template v-if="column.key === 'internalSchedule'">
            <div>
              {{ column.customTitle }}
              <HoverDescription @change="(v) => change(v, 'inside')" />
            </div>
          </template>
          <template v-else>
            <HeaderCell :column="column" />
          </template>
        </template>
        <template #bodyCell="{ column, text, record }">
          <!-- 当月进度 -->
          <template v-if="column.dataIndex === 'internalSchedule'">
            <div class="item-cloumn pad-r">
              <Progress
                :percent="text * 1"
                :status="setStatus(text)"
                :trailColor="trailColor"
                :strokeColor="strokeColor"
              />
              <Tendency
                :state="record.internalScheduleState"
                :data="record.internalScheduleData"
                :checked="record.checked"
              />
            </div>
          </template>
          <!-- 累计进度 -->
          <template v-if="column.dataIndex === 'totalInternalSchedule'">
            <div class="pad-r item-cloumn">
              <Progress
                :percent="text * 1"
                :trailColor="trailColor"
                :strokeColor="strokeColor"
                :status="setStatus(text)"
              />
              <Tendency
                :state="record.internalTotalScheduleState"
                :data="record.internalTotalScheduleData"
                :checked="record.checked"
              />
            </div>
          </template>
          <!-- 是否变更预算 -->
          <template v-if="column.dataIndex === 'changeFlag'">
            <div class="change-text" :class="{ change: changListText[text].value }">
              {{ changListText[text].label }}
              <a
                @click="handlePop($event, record)"
                href="javascript:;"
                style="color: #1153d8; margin-left: 8px"
                v-if="changListText[text].value"
                >详情</a
              >
            </div>
          </template>
        </template>
      </BasicTable>
    </div>
    <div v-show="outMaterial">
      <FormTitle
        title="红线外进度"
        @change="(val) => toggle(val, 'outside')"
        :showToggle="!!list2.length"
      />
      <BasicTable @register="registerTables">
        <template #headerCell="{ column }">
          <template v-if="column.key === 'schedule'">
            <div>
              {{ column.customTitle }}
              <HoverDescription @change="(v) => change(v, 'outside')" />
            </div>
          </template>
          <template v-else>
            <HeaderCell :column="column" />
          </template>
        </template>
        <template #bodyCell="{ column, text, record }">
          <!-- 当月进度 -->
          <template v-if="column.dataIndex === 'schedule'">
            <div class="item-cloumn pad-r">
              <Progress
                :percent="text * 1"
                :status="setStatus(text)"
                :trailColor="trailColor"
                :strokeColor="strokeColor"
                size="small"
              />
              <Tendency
                :state="record.scheduleState"
                :data="record.scheduleData"
                :checked="record.checked"
              />
            </div>
          </template>
          <!-- 累计进度 -->
          <template v-if="column.dataIndex === 'totalSchedule'">
            <div class="pad-r item-cloumn">
              <Progress
                :percent="text * 1"
                :trailColor="trailColor"
                :strokeColor="strokeColor"
                :status="setStatus(text)"
              />
              <Tendency
                :state="record.totalScheduleState"
                :data="record.totalScheduleData"
                :checked="record.checked"
              />
            </div>
          </template>
          <!-- 是否变更预算 -->
          <template v-if="column.dataIndex === 'changeFlag'">
            <div class="change-text" :class="{ change: changListText[text].value }">
              {{ changListText[text].label }}
              <a
                @click="handlePop($event, record)"
                href="javascript:;"
                style="color: #1153d8; margin-left: 8px"
                v-if="changListText[text].value"
                >详情</a
              >
            </div>
          </template>
        </template>
      </BasicTable>
    </div>
    <div class="no-data" v-if="!inMaterial && !outMaterial">
      <div class="empty-img"><img :src="emptyImg" /></div>
      <div>暂无数据</div>
    </div>
  </div>
</template>

<script setup lang="ts" name="collect">
  import { onMounted, ref, unref } from 'vue';
  import { Progress } from 'ant-design-vue';
  // import { Spin } from 'ant-design-vue';
  import emptyImg from '/@/assets/images/table-empty.png';
  import { useComplexContext } from '/@/components/HComplexPage/useComplexContext';

  import HoverDescription from './components/HoverDescription.vue';
  import Tendency from './components/Tendency.vue';
  import HeaderCell from '/@/components/Table/src/components/HeaderCell.vue';
  import { BasicTable, useTable } from '/@/components/Table';
  import { tableColumn, tableColumn2 } from './index.data';
  import FormTitle from './components/FormTitle.vue';
  import {
    GetScheduleList,
    getSearchMaterialInStatus,
    getSearchMaterialOutStatus,
  } from '/@/api/zkrq/projectManagement';

  const emit = defineEmits(['detail']);

  const props = defineProps({
    setActiveIndex: {
      type: Function,
      default: () => {},
    },
    modelId: {
      type: String,
      default: '',
    },
    dataSource: {
      type: Array as PropType<any[]>,
      default: () => [],
    },
  });
  const loading = ref(true);
  const { record } = useComplexContext();
  const trailColor = 'rgba(216,216,216,0.3)';
  const strokeColor = '#1153D8';
  let insideFlag = false;
  let outsideFlag = false;

  const inMaterial = ref(true);
  const outMaterial = ref(true);

  const changListText = [
    { label: '未变更', value: 0 },
    { label: '已变更', value: 1 },
  ];
  // 查询红线内外材料显示
  const searchMaterialInorOut = async () => {
    inMaterial.value = await getSearchMaterialInStatus({ projectId: record?.project_id ?? '' });
    outMaterial.value = await getSearchMaterialOutStatus({ projectId: record?.project_id ?? '' });

    getList(inMaterial.value, outMaterial.value);
  };

  const [registerTable, { setTableData }] = useTable({
    columns: tableColumn,
    showIndexColumn: false,
    pagination: false,
  });

  const [registerTables, { setTableData: setTableDatas }] = useTable({
    columns: tableColumn2,
    showIndexColumn: false,
    pagination: false,
  });
  const list = ref([]);
  const list2 = ref([]);
  // 获取列表
  const getList = async (inMaterial: boolean, outMateria: boolean) => {
    const ret = await GetScheduleList({ projectId: (record && record.project_id) || '' });
    loading.value = false;
    if (inMaterial) {
      list.value = (ret.list1 || []).map((i) => {
        i.checked = false;
        return i;
      });
      toggle(false, 'inside');
    }
    if (outMateria) {
      list2.value = (ret.list2 || []).map((i) => {
        i.checked = false;
        return i;
      });
      toggle(false, 'outside');
    }
  };

  onMounted(() => {
    searchMaterialInorOut();
  });

  const setStatus = (value: number) => (value === 100 ? 'success' : 'active');

  const toggle = (val: boolean, type: string, num = 1) => {
    if (type === 'inside') {
      insideFlag = val;
      if (!val) {
        const arr = unref(list).slice(0, num);
        setTableData(arr);
        return;
      }

      setTableData(unref(list));
    } else {
      outsideFlag = val;
      if (!val) {
        const arr = unref(list2).slice(0, num);
        setTableDatas(arr);
        return;
      }

      setTableDatas(unref(list2));
    }
  };

  // 月份比对
  const change = (val: boolean, type: string) => {
    let flag = false;
    if (type === 'inside') {
      list.value.forEach((i) => {
        i.checked = val;
      });
      flag = insideFlag;
    } else {
      flag = outsideFlag;
      list2.value.forEach((i) => {
        i.checked = val;
      });
    }

    toggle(flag, type);
  };
  function getMonthFinalDay(year, month) {
    var day;
    if (year == null || year == undefined || year == '') {
      year = new Date().getFullYear();
    }
    if (month == null || month == undefined || month == '') {
      month = new Date().getMonth() + 1;
    }
    day = new Date(new Date(year, month).setDate(0)).getDate();
    return year + '-' + month + '-' + day;
  }

  // 详情弹窗
  const handlePop = (_, record) => {
    let endDay;
    let startDay;
    if (record.monthTime) {
      let arr = record.monthTime.split('-');
      startDay = `${record.monthTime}-01`;
      endDay = getMonthFinalDay(arr[0], arr[1]);
    }

    emit('detail', { WorkItemName: '设计变更', CreateDate: [startDay, endDay] });
    const tabsIndex = props.dataSource.findIndex((i) => i.modelId === '171');

    if (tabsIndex > -1) {
      props.setActiveIndex(tabsIndex);
    }
  };
</script>

<style lang="less" scoped>
  :deep(.ant-progress-status-success .ant-progress-bg) {
    background-color: #55d187 !important;
  }

  .item-cloumn {
    display: flex;

    :deep(.ant-progress) {
      width: 140px;

      .ant-progress-inner {
        height: 16px !important;

        .ant-progress-bg {
          height: 100% !important;
        }
      }
    }
  }

  .pad-r {
    padding-right: 30px;
  }

  .change-text {
    display: flex;
    align-items: center;

    &::before {
      content: '';
      width: 6px;
      height: 6px;
      border-radius: 50%;
      display: block;
      background: #4db803;
      margin-right: 8px;
    }
  }

  .change {
    &::before {
      background: #ff522b;
    }
  }

  :deep(.vben-basic-table--empty .ant-table-wrapper .ant-table-body) {
    padding-top: 0 !important;
  }

  :deep(.vben-basic-table) {
    height: auto !important;
  }

  :deep(.ant-table-container) {
    .ant-table-body {
      height: auto !important;
    }
  }

  .no-data {
    width: 120px;
    text-align: center;
    margin: 100px auto;
    color: #ccc;
  }
</style>
